package com.rykj.api.converter;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;

import java.io.IOException;

import okhttp3.ResponseBody;
import okio.BufferedSource;
import okio.Okio;
import retrofit2.Converter;

/**
 *
 */
final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {

    private static final String TAG = "GsonResponseBodyConvert";

    private final TypeAdapter<T> adapter;

    private final Gson gson;

    GsonResponseBodyConverter(Gson gson,TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        BufferedSource bufferedSource = Okio.buffer(value.source());
        T temp = null;
        try {
            String json = bufferedSource.readUtf8();
            //<editor-fold desc="解析加密数据使用">
//            json = RSAUtils.deCodeKey(json);
//            json = new String(Base64.decode(json), "UTF-8");
            //</editor-fold>
            //URLDecoder.decode(json, "UTF-8");
            temp = adapter.fromJson(json);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            bufferedSource.close();
        }
        return temp;
    }
}